package com.erp.module.YuliaoPrint

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.YuliaoPrintBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.ArrayList

class IYuLiaoPrintP(var list :MutableList<YuliaoPrintBean>, var V : YuliaoPrintContract.YuliaoPrintView, var context: Context) : BasePullP(), YuliaoPrintContract.YuliaoPrintP{


    var md = YuliaoPrintModel()
    var sousuoBean = FragmengtYuliaoPrintLabel.SousuoBean()
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        if(sousuoBean.mc_id == ""){
            Myutil.MyTost("物料类别必选")
            V.xialaNo()
            V.shanglaNo()
            return
        }
        if(sousuoBean.mb_area_num == ""){
            Myutil.MyTost("区号必须填写")
            V.xialaNo()
            V.shanglaNo()
            return
        }
        md.getYuliaoList(context,page,pagesize,sousuoBean,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        if (data==null){
            return
        }
        try {
            val j = JSONObject(data)
            val js = JsonUtil.getjsvolue(j,"data")
            for (i in 0 until js.length()) {
                val bean = YuliaoPrintBean()
                val jo = js.get(i) as JSONObject
                bean.mid = JsonUtil.getvolue(jo, "mid")
                bean.mb_batch = JsonUtil.getvolue(jo, "mb_batch")
                bean.mb_area_num = JsonUtil.getvolue(jo, "mb_area_num")
                bean.m_name = JsonUtil.getvolue(jo, "m_name")
                bean.color_color = JsonUtil.getvolue(jo, "color_color")
                bean.m_size = JsonUtil.getvolue(jo, "m_size")
                bean.es_type = JsonUtil.getvolue(jo, "es_type")
                bean.mm_menjiaokong = JsonUtil.getvolue(jo, "mm_menjiaokong")
                bean.weihao = JsonUtil.getvolue(jo, "weihao")
                bean.mcode = JsonUtil.getvolue(jo, "mcode")
                bean.finished_size = JsonUtil.getvolue(jo, "finished_size")
                bean.es_dnum_mark = JsonUtil.getvolue(jo, "es_dnum_mark")
                bean.mb_productive_time = JsonUtil.getvolue(jo, "mb_productive_time")

                bean.position = JsonUtil.getvolue(jo, "position")
                bean.kailiao_time = JsonUtil.getvolue(jo, "kailiao_time")
                bean.kailiao_wu_id = JsonUtil.getvolue(jo, "kailiao_wu_id")
                bean.kailiao_name = JsonUtil.getvolue(jo, "kailiao_name")
                bean.slot_name = JsonUtil.getvolue(jo, "slot_name")
                bean.m_remark = JsonUtil.getvolue(jo, "m_remark")
                bean.m_counts = JsonUtil.getvolue(jo, "m_counts")
                bean.color_code = JsonUtil.getvolue(jo, "color_code")
                bean.ban_jian_hao = JsonUtil.getvolue(jo, "ban_jian_hao")

                list.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun sousuo(sousuo: FragmengtYuliaoPrintLabel.SousuoBean) {
        this.sousuoBean=sousuo
        xiala()
    }

    override fun getcategory() {
        var zhiliangid: MutableList<String> = ArrayList()
        var zhiliangname: MutableList<String> = ArrayList()
        md.getWuLiao_LeiBie_DaiMa(context,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                val jo = JSONObject(data)
                val keys = jo.keys()
                while (keys.hasNext()) {
                    val key = keys.next()
                    zhiliangid.add(key)
                    zhiliangname.add(JsonUtil.getvolue(jo, key))
                }
                V.getcategoryOk(zhiliangid,zhiliangname)
            }

            override fun onNot(throwable: String?) {
                V.getcategoryNo()
            }

        })
    }


}